package com.ruoyi.web.controller.module.setMeal;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.setMeal.SetMeal;
import com.ruoyi.system.domain.module.setMeal.SetMealInfo;
import com.ruoyi.system.domain.module.setMeal.SetMealInfoGiven;
import com.ruoyi.system.service.module.setMeal.ISetMealInfoService;
import com.ruoyi.system.service.module.setMeal.ISetMealService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.module.setMeal.ISetMealInfoGivenService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 套餐规格下的 赠送消费券金额Controller
 *
 * @author ruoyi
 * @date 2022-06-07
 */
@Validated
@Api(value = "套餐规格下的 赠送消费券金额控制器", tags = {"套餐规格下的 赠送消费券金额管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/meal/info/given")
public class SetMealInfoGivenController extends BaseController {

    private final ISetMealInfoGivenService setMealInfoGivenService;
	private final ISetMealInfoService setMealInfoService;
	private final ISetMealService setMealService;

    /**
     * 查询套餐规格下的 赠送消费券金额列表
     */
    @ApiOperation("查询套餐规格下的 赠送消费券金额列表")
    @PreAuthorize("@ss.hasPermi('module:mealInfoGiven:list')")
    @GetMapping("/list")
    public TableDataInfo<SetMealInfoGiven> list(@Validated SetMealInfoGiven setMealInfoGiven) {
        QueryWrapper<SetMealInfoGiven> qw = new QueryWrapper<>();
        if(setMealInfoGiven.getMealInfoId() != null){
        	qw.eq("meal_info_id",setMealInfoGiven.getMealInfoId());
		}

        TableDataInfo<SetMealInfoGiven> td = PageUtils.buildDataInfo(setMealInfoGivenService.page(PageUtils.buildPage(),qw));
        List<SetMealInfoGiven> list = td.getRows();
        return td;
    }

    /**
     * 导出套餐规格下的 赠送消费券金额列表
     */
    @ApiOperation("导出套餐规格下的 赠送消费券金额列表")
    @PreAuthorize("@ss.hasPermi('module:mealInfoGiven:export')")
    @Log(title = "套餐规格下的 赠送消费券金额", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<SetMealInfoGiven> export(@Validated SetMealInfoGiven setMealInfoGiven) {
        List<SetMealInfoGiven> list = setMealInfoGivenService.list();
        ExcelUtil<SetMealInfoGiven> util = new ExcelUtil<SetMealInfoGiven>(SetMealInfoGiven.class);
        return util.exportExcel(list, "套餐规格下的 赠送消费券金额");
    }

    /**
     * 获取套餐规格下的 赠送消费券金额详细信息
     */
    @ApiOperation("获取套餐规格下的 赠送消费券金额详细信息")
    @PreAuthorize("@ss.hasPermi('module:mealInfoGiven:query')")
    @GetMapping("/{id}")
    public AjaxResult<SetMealInfoGiven> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        SetMealInfoGiven setMealInfoGiven = setMealInfoGivenService.getById(id);
        if(setMealInfoGiven!=null){
			SetMeal setMeal = setMealService.findById(setMealInfoGiven.getMealId());
			if(setMeal!=null){
				setMealInfoGiven.setMealName(setMeal.getMealName());
			}

			SetMealInfo setMealInfo = setMealInfoService.findById(setMealInfoGiven.getMealInfoId());
			if(setMealInfo!=null){
				setMealInfoGiven.setMealInfoName(setMealInfo.getMealInfoName());
			}
		}
        return AjaxResult.success(setMealInfoGiven);
    }

    /**
     * 新增套餐规格下的 赠送消费券金额
     */
    @ApiOperation("新增套餐规格下的 赠送消费券金额")
    @PreAuthorize("@ss.hasPermi('module:mealInfoGiven:add')")
    @Log(title = "套餐规格下的 赠送消费券金额", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody SetMealInfoGiven setMealInfoGiven) {
        return toAjax(setMealInfoGivenService.save(setMealInfoGiven) ? 1 : 0);
    }

    /**
     * 修改套餐规格下的 赠送消费券金额
     */
    @ApiOperation("修改套餐规格下的 赠送消费券金额")
    @PreAuthorize("@ss.hasPermi('module:mealInfoGiven:edit')")
    @Log(title = "套餐规格下的 赠送消费券金额", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody SetMealInfoGiven setMealInfoGiven) {
        return toAjax(setMealInfoGivenService.updateById(setMealInfoGiven) ? 1 : 0);
    }

    /**
     * 删除套餐规格下的 赠送消费券金额
     */
    @ApiOperation("删除套餐规格下的 赠送消费券金额")
    @PreAuthorize("@ss.hasPermi('module:mealInfoGiven:remove')")
    @Log(title = "套餐规格下的 赠送消费券金额" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(setMealInfoGivenService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
